<?php

namespace App\Services;

use App\Models\BaseModel;
use App\Models\Notice;
use App\Models\Order;
use Illuminate\Support\Facades\DB;

class NoticeService extends BaseService
{
    /**
     * 通知列表
     * @param $page
     * @param $pageSize
     * @param $uid
     * @param $type
     * @return mixed
     */
    public function list($page, $pageSize, $uid, $type)
    {
        // 设置对象
        $model = Notice::query();
        // 类型
        $model = $model->where('type', $type);
        // 用户
        if ($type != Notice::TYPE_SYSTEM) {
            $model = $model->where('user_id', $uid);
        }
        // 获取列表
        $result = $model
            ->orderBy('is_read')
            ->orderBy('id', 'desc')
            ->paginate($pageSize, ['id', 'type', 'type_id', 'title', 'con', 'is_read', 'created_at'], 'page', $page);

        $orderList = [];
        if ($type == Notice::TYPE_ORDER && $typeIds = $result->getCollection()->pluck('type_id')) {
            $typeIds = array_filter($typeIds->toArray());
            $orderList = Order::query()->whereIn('id', $typeIds)->get(['id', 'sell_json', 'write_off_time', 'state'])->toArray() ?? [];
            $orderList = array_column($orderList, null, 'id');
        }
        $result->getCollection()->transform(function ($item) use ($orderList) {
            $item = json_decode($item, true);
            if ($item['type'] == Notice::TYPE_ORDER) {
                $item['sell_json'] = json_decode($orderList[$item['type_id']]['sell_json'], true) ?? '';
                $item['use_time'] = $orderList[$item['type_id']]['write_off_time'] ?? '';
                $item['state'] = $orderList[$item['type_id']]['state'] ?? '';
            }
            $item['created_at'] = timeTran($item['created_at']);
            return $item;
        });

        // 变成已读
        if ($type == Notice::TYPE_ORDER) {
            Notice::where(['type' => Notice::TYPE_ORDER, 'user_id' => $uid, 'is_read' => BaseModel::NO])->update(['is_read' => BaseModel::YES]);
        } elseif ($type == Notice::TYPE_SERVICE) {
            Notice::where(['type' => Notice::TYPE_SERVICE, 'user_id' => $uid, 'is_read' => BaseModel::NO])->update(['is_read' => BaseModel::YES]);
        }

        // 未读数量
        $orderUnReadCount = Notice::query()->where(['type' => Notice::TYPE_ORDER, 'user_id' => $uid, 'is_read' => BaseModel::NO])->count('id');
        $serviceUnReadCount = Notice::query()->where(['type' => Notice::TYPE_SERVICE, 'user_id' => $uid, 'is_read' => BaseModel::NO])->count('id');

        return array_merge($this->successPage($result), ['order_un_read_count' => $orderUnReadCount, 'service_un_read_count' => $serviceUnReadCount]);
    }

    /**
     * 添加通知
     * @param $type
     * @param $typeId
     * @param $uid
     * @param $title
     * @param $con
     * @return void
     */
    public function add($type, $typeId, $uid, $title, $con = null)
    {
        $noticeData = [
            'type' => $type,
            'type_id' => $typeId,
            'user_id' => $uid,
            'title' => $title,
            'con' => $con,
            'is_read' => BaseModel::NO,
            'created_at' => date('Y-m-d H:i:s'),
        ];
        DB::table('notice')->insertGetId($noticeData);
    }
}
